package ed.hbut.supermarket1.controller;

import ed.hbut.supermarket1.entity.Goods;
import ed.hbut.supermarket1.entity.Result;
import ed.hbut.supermarket1.service.GoodsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
public class GoodsController {

    @Autowired
    private GoodsService goodsService;


    /**
     * 出库
     */
/*
    @PutMapping("/api/goods/{id}")
    public Result updateGoods(@PathVariable int id) {



        return Result.success();
    }
*/

    /**
     * 查询所有的货品
     * @return
     */
    @GetMapping("/api/goods")
    public Result getAllGoods() {

        List<Goods> list = goodsService.getGoods();
        return Result.success(list);
    }

    /**
     * 新增货品
     * @param goods
     * @return
     */
    @PostMapping("/api/goods")
    public Result addGoods(@RequestBody Goods goods) {



        goodsService.addGoods(goods);

        return Result.success();
    }


    /**
     * 根据id查询货品
     * @param id
     * @return
     */
    @GetMapping("/api/goods/{id}")
    public Result getGoods(@PathVariable int id) {

        Goods goods = goodsService.getGoodsById(id);

        return  Result.success(goods);
    }

    /**
     * 修改货品信息
     * @param id
     * @param goods
     * @return
     */
    @PutMapping("/api/goods/{id}")
    public Result updateGoods(@PathVariable int id, @RequestBody Goods goods) {

        goodsService.updateGoods(id,goods);
        return Result.success();
    }

    /**、
     * 删除货品
     * @param id
     * @return
     */
    @DeleteMapping("/api/goods/{id}")
    public Result deleteGoods(@PathVariable int id) {

        log.info("delete goods id:{}",id);
        goodsService.deleteGoods(id);

        return Result.success();
    }
}
